--[[
    队列
--]]

local List = {}

function List.new()
    return {first = 0, last = -1}
end

function List.pushFront(list, value)
    local first = list.first - 1
    list.first = first
    list[first] = value
end

function List.pushBack(list, value)
    local last = list.last + 1
    list.last = last
    list[last] = value
end

function List.popFront(list)
    local first = list.first
    if first > list.last then
        --error("List is empty")
        return nil
    end
    local value = list[first]
    list[first] = nil
    list.first = first + 1
    return value
end

function List.popBack(list)
    local last = list.last
    if list.first > last then
        --error("List is empty")
        return nil
    end
    local value = list[last]
    list[last] = nil
    list.last = last - 1
    return value
end

function List.queryFront(list, count)
    local first = list.first
    if (first + count) > list.last then
        return nil
    end
    local data = {}
    for i = first, (first + count - 1) do
        table.insert(data, list[i])
    end
    return data
end

function List.popValue(list, value)
    local last = list.last
    if list.first > last then
        return nil
    end
    for i=list.first, last do
        if list[i] == value then
            list[i] = nil
            --删除
            for j = i, last-1 do
                list[j] = list[j+1]
            end
            list[last] = nil
            list.last = last - 1
        end
    end
    return true
end

return List